package com.sh.rbac.controller;

import com.sh.rbac.annotation.OperationLogger;
import com.sh.rbac.domain.dto.menu.MenuQueryDTO;
import com.sh.rbac.domain.dto.menu.MenuSaveDTO;
import com.sh.rbac.domain.dto.menu.MenuUpdateDTO;
import com.sh.rbac.domain.entity.Menu;
import com.sh.rbac.domain.enums.OperationTypeEnum;
import com.sh.rbac.domain.vo.menu.MenuNavVO;
import com.sh.rbac.result.Result;
import com.sh.rbac.service.MenuService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * 菜单控制器
 *
 * @author lucky
 * @since 2024-04-06
 */

@Tag(name = "菜单管理", description = "菜单相关操作")
@RequestMapping("sys/menu")
@RestController
public class MenuController {

    @Autowired
    private MenuService menuService;


    @Operation(summary = "查询菜单列表")
    @ApiResponse(responseCode = "200", description = "返回查询结果", content = @Content(schema = @Schema(implementation = MenuNavVO.class)))
    @GetMapping("nav")
    public Result<List<MenuNavVO>> getMenu() {
        return Result.success(menuService.getMenus());
    }


    @Operation(summary = "查询菜单权限")
    @ApiResponse(responseCode = "200", description = "返回查询结果", content = @Content(schema = @Schema(implementation = String.class)))
    @GetMapping("auth")
    public Result<Set<String>> auth() {
        return Result.success(menuService.getAuth());
    }

    @Operation(summary = "查询菜单列表", description = "根据菜单条件查询参数进行查询")
    @ApiResponse(responseCode = "200", description = "返回查询结果", content = @Content(schema = @Schema(implementation = Menu.class)))
    @PostMapping("list")
    @PreAuthorize("hasAuthority('sys:menu:list')")
    public Result<List<Menu>> getList(@RequestBody MenuQueryDTO menuQueryDTO) {
        return Result.success(menuService.getList(menuQueryDTO));
    }

    @OperationLogger(module = "系统管理->菜单管理", type = OperationTypeEnum.ADD)
    @Operation(summary = "添加菜单", description = "保存一个新菜单")
    @ApiResponse(responseCode = "200", description = "成功保存菜单")
    @PostMapping("save")
    @PreAuthorize("hasAuthority('sys:menu:save')")
    public Result<Boolean> save(@RequestBody MenuSaveDTO menuSaveDTO) {
        return menuService.save(menuSaveDTO) ? Result.success() : Result.failure();
    }


    @OperationLogger(module = "系统管理->菜单管理", type = OperationTypeEnum.UPDATE)
    @Operation(summary = "更新菜单", description = "根据ID更新菜单信息")
    @ApiResponse(responseCode = "200", description = "成功更新菜单")
    @PutMapping("update")
    @PreAuthorize("hasAuthority('sys:menu:update')")
    public Result<Boolean> update(@RequestBody MenuUpdateDTO menuUpdateDTO) {
        return menuService.updateById(menuUpdateDTO) ? Result.success() : Result.failure();
    }


    @OperationLogger(module = "系统管理->菜单管理", type = OperationTypeEnum.DELETE)
    @Operation(summary = "删除菜单", description = "根据ID删除菜单信息")
    @ApiResponse(responseCode = "200", description = "成功删除菜单")
    @DeleteMapping("delete/{id}")
    @PreAuthorize("hasAuthority('sys:menu:delete')")
    public Result<Boolean> delete(@PathVariable Long id) {
        return menuService.removeById(id) ? Result.success() : Result.failure();
    }


    @Operation(summary = "根据ID查询菜单")
    @ApiResponse(responseCode = "200", description = "返回查询结果", content = @Content(schema = @Schema(implementation = Menu.class)))
    @GetMapping("get/{id}")
    @PreAuthorize("hasAuthority('sys:menu:get')")
    public Result<Menu> get(@PathVariable Long id) {
        return Result.success(menuService.getById(id));
    }


    @OperationLogger(module = "系统管理->菜单管理", type = OperationTypeEnum.BATCH_DELETE)
    @Operation(summary = "批量删除菜单", description = "批量删除菜单信息")
    @ApiResponse(responseCode = "200", description = "成功删除菜单")
    @DeleteMapping("batch-delete")
    @PreAuthorize("hasAuthority('sys:menu:batch-delete')")
    public Result<Boolean> batchDelete(@RequestBody List<Long> ids) {
        return menuService.removeBatchByIds(ids) ? Result.success() : Result.failure();
    }

    @OperationLogger(module = "系统管理->菜单管理", type = OperationTypeEnum.UPDATE)
    @Operation(summary = "更新菜单状态", description = "根据ID更新菜单状态")
    @ApiResponse(responseCode = "200", description = "成功更新菜单状态")
    @PutMapping("update-status/{id}/{status}")
    @PreAuthorize("hasAuthority('sys:menu:update-status')")
    public Result<Boolean> updateStatus(@PathVariable Long id, @PathVariable Integer status) {
        return menuService.updateStatus(id, status) ? Result.success() : Result.failure();
    }
}
